package com.gec.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.utils.StringUtils;
import com.gec.system.service.MsmService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {


    //发送短信的方法
    @Override
    public boolean sendMsm(HashMap<String, Object> map, String phone) {
        if (StringUtils.isEmpty(phone))return false;

        //参数1地域节点
        //参数2AccessKey ID
        //参数3AccessKey Secret
        DefaultProfile profile = DefaultProfile.getProfile("cn-guangzhou", "LTAI5tEFBQRfsPdcduLeR4SL", "nZmi4EUdP150ffMVbMkhwbW20FrxoQ");
        DefaultAcsClient client = new DefaultAcsClient(profile);

        //设置相关固定参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType.POST); //提交方式默认不能改
        request.setSysDomain("dysmsapi.aliyuncs.com");//请求阿里云哪里默认不能改
        request.setSysVersion("2017-05-25");//版本号
        request.setSysAction("SendSms");//请求哪个方法


        //设置发送相关参数
        request.putQueryParameter("PhoneNumbers",phone);//设置要发送的【手机号】
        request.putQueryParameter("SignName","小宇物业管理系统");//申请阿里云短信服务的【签名名称】
        request.putQueryParameter("TemplateCode","SMS_464220772");//申请阿里云短信服务的【模版中的 模版CODE】


        //要求传递的code验证码为jason格式可以使用JSONObject.toJSONString()将map转为json格式
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));


        //最终发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }



        @Override
        public boolean send(Map<String, Object> param, String phone) {
            if (StringUtils.isEmpty(phone)) {
                return false;
            }

            DefaultProfile profile = DefaultProfile.getProfile("cn-guangzhou", "LTAI5tEFBQRfsPdcduLeR4SL", "nZmi4EUdP150ffMVbMkhwbW20FrxoQ");//自己账号的AccessKey信息
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");//短信服务的服务接入地址

            request.setSysVersion("2017-05-25");//API的版本号
            request.setSysAction("SendSms");//API的名称

            request.putQueryParameter("PhoneNumbers", phone);//接收短信的手机号码
            request.putQueryParameter("SignName", "小宇物业管理系统");//短信签名名称
            request.putQueryParameter("TemplateCode", "SMS_464220772");//短信模板ID
            request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//短信模板变量对应的实际值

            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
                boolean success = response.getHttpResponse().isSuccess();
                return success;
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }

            return false;
//    }

//        String to =phone+"@qq.com";//收件人
//        System.out.println("向"+to+"发送邮件");
//        String from = "3150847886@qq.com";//发送人邮箱必须开启..？？？.服务
//        String subject = "验证码邮件";
//        String context = "阿里云短信服务没钱用====>验证码"+param;
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(from);
//        message.setText(context);
//        message.setTo(to);
//        message.setSubject(subject);
//        javaMailSender.send(message);
//        System.out.println("成功发送邮件---->");
//        return true;


    }
}
